<?php
/**
 * @file emodecontroller.php
 * @author LinLin
 * @date 2016年3月14日 上午10:24:54
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\emode;

use haibao\frame\data\query\Condition;
use haibao\cms\data\emode\EmodeArticle as EmodeArticleData;
use haibao\cms\data\emode\EmodeBrand as EmodeBrandData;

class EmodeController extends \haibao\cms\business\control\pc\channel\ChannelController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $articleInfo = $productList = $articleLists = $brandList = $articleIdArr = array();
        $lastArticleId = '';
        //文章列表
        if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'articleLists'){
            $data = new EmodeArticleData();
            $articleArr = $data->getEmodeArticle(1,'');
            $articleInfo = $articleId = '';
            if($articleArr){
                foreach($articleArr as $key=>$val){
                    if($key == 0){
                        $articleId = $val['Id'];
                    }
                    array_push($articleIdArr,$val['PublishTime']);
                }
                $lastArticleId = array_pop($articleIdArr);
                $articleLists = $this->covertImageUrl($articleArr,300,300);
                if($articleId){
                    $articleInfo = $data->getOneArticleById($articleId);
                    $publishTime = date($articleInfo['PublishTime']);
                    $articleInfo['PublishTime'] = date('Y-m-d',strtotime($publishTime));
                }
            }
            $this->assign('lastArticleId', $lastArticleId);
            $this->assign('articleInfo', $articleInfo);
            $this->assign('articleLists', $articleLists);
        }
        
        //品牌单品
        if($controlData['manual'] && $controlData['manual']['contorllerType'] == 'productAndBrand'){
            $data = new EmodeBrandData();
            $brandList = $data->getEmodeBrand();
            $brandList = $this->covertImageUrl($brandList,85,50);
            
            $productList = $data->getEmodeProductByBrandId();
            $productList = $this->covertImageUrl($productList,400,400);

            $this->assign('brandList', $brandList);
            $this->assign('productList', $productList);
        }
        $this->assign('EmodeUrl', 'https://shop130741631.taobao.com/');
        $this->assign('EmodeProductUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/features/brand/EMODE_1/');
        $isNewHtml = $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    
    //通过图片ID 获取图片地址
    public function covertImageUrl($arr,$width,$height){
        $imageIds = array();
        foreach($arr as $key=>$val){
            array_push($imageIds,$val['ImageId']);
        }
        if($imageIds){
            $fileNames = \haibao\cms\business\ImageLibrary::getImageUrlById($imageIds);
        }
        $article = new \haibao\cms\common\ArticleContent();
        foreach ($arr as $key=>$model){
            if(isset($fileNames[$model['ImageId']])){
                $staticUrl = $article->replaceImageLink($fileNames[$model['ImageId']], $width, $height, 100, 0);
                $arr[$key]['ImageUrl'] = $article->replaceImgUrlCDNLink($staticUrl);
            }
        }
        return $arr;
    }
}